const sqlmap = {
  Admin: {
    adminDel: (id) => {
      return `delete from admin where id = ${id}`;
    },
    adminAdd: (data) => {
      return `insert into admin (id,account,password,email,weight) values (${data
        .map((item) => `'${item}'`)
        .join(",")})`;
    },
    adminByAccount: (account) => {
      return `select id,account,password,email,weight from admin where account like '%${account}%'`;
    },
    adminNameQuery: (account) => {
      return `select id,account,password,email,weight from admin where account = '${account}'`;
    },
    adminUpdate: (data) => {
      return `
        update admin set account = '${data.account}',password = '${data.password}',
        email = '${data.email}',weight = ${data.weight} where id = '${data.id}'
      `;
    },
    adminUpdateActive: (data) => {
      return `
      update admin set weight = ${data.weight} where id = '${data.id}'
    `;
    },
  },
  Space: {
    addfilespace: (data) => {
      return `insert into fileservespacetable (space_id,space_name) values (${data
        .map((item) => `'${item}'`)
        .join(",")})`;
    },
    updatespace: (data) => {
      return `update fileservespacetable set space_name = '${data.spaceName}' where space_id = '${data.spaceId}'`;
    },
    delspace: (spaceId) => {
      return `delete from fileservespacetable where space_id = '${spaceId}'`;
    },
    getspacelist: () => {
      return `select id,space_id,space_name from fileservespacetable`;
    },
    getSpaceInfo: (spaceName) => {
      return `select id,space_id,space_name from fileservespacetable where space_name = '${spaceName}'`;
    },
  },
  Own: {
    //根据文件名查询文件访问状态
    fileStatus:(fileName,spaceId)=>{
      return `select status from fileinformationtable where fileName = '${fileName}' and spaceId = ${spaceId}`; 
    },
    isReuseFile: (fileName, spaceId) => {
      return (
        `select id,fileId,fileName,fileSize,fileType,remark from fileinformationtable where fileName = '${fileName}'` +
        (spaceId ? " and spaceId = " + spaceId : "")
      );
    },
    fileAdd: (data) => {
      return `insert into fileinformationtable (fileId,spaceId,fileName,fileSize,fileType,remark,status,addressOrgin) values (${data
        .map((item) => `'${item}'`)
        .join(",")})`;
    },
    fileDel: (fileId) => {
      return `delete from fileinformationtable where fileId = ${fileId}`;
    },
    fileUpdate: (data) => {
      return `
        update fileinformationtable set ${Object.keys(data)
          .map((item) => `${item} = '${data[item]}'`)
          .join(",")} where fileId = '${data.fileId}'
      `;
    },
    fileQuery: (fileId) => {
      return `select id,fileId,spaceId,fileName,fileSize,fileType,remark from fileinformationtable where fileId = '${fileId}'`;
    },
    filePageQuery: (page, limit, table, spaceId, keyword) => {
      const offset = (page - 1) * limit;
      let sql = `select * from ${table}`;
      if (spaceId !== undefined) {
        sql += ` where spaceId = ${spaceId}`;
        if (keyword !== undefined) {
          sql += ` and (fileName like '%${keyword}%' or remark like '%${keyword}%')`;
        }
      } else {
        if (keyword !== undefined) {
          sql += ` where (fileName like '%${keyword}%' or remark like '%${keyword}%')`;
        }
      }

      sql += ` limit ${limit} offset ${offset}`;
      return sql;
    },
    fileQueryTotal: (table, spaceId, keyword) => {
      let sql = `select count(*) from ${table}`;
      if (spaceId !== undefined) {
        sql += ` where spaceId = ${spaceId}`;
        if (keyword !== undefined) {
          sql += ` and (fileName like '%${keyword}%' or remark like '%${keyword}%')`;
        }
      } else {
        if (keyword !== undefined) {
          sql += ` where (fileName like '%${keyword}%' or remark like '%${keyword}%')`;
        }
      }
      return sql;
    },
  },

  /**
   * 分页查询
   * @param {number} page 查询页数
   * @param {number} limit 查询条数
   * @param {string} table 查询表
   * @returns {string} 返回值为string类型
   */
  pagingQuery: (page, limit, table) => {
    const offset = (page - 1) * limit;
    return `select * from ${table} limit ${limit} offset ${offset}`;
  },
  pagingTotal: (table) => {
    return `select count(*) from ${table}`;
  },
};
const { Admin, pagingQuery, pagingTotal, Own, Space } = sqlmap;
module.exports = { Admin, Own, Space, pagingQuery, pagingTotal };
